package com.wynn.service.impl;

import com.wynn.dao.UserDao;
import com.wynn.service.UserService;
import com.wynn.pojo.User;
import com.wynn.pojo.Result;
import org.python.antlr.op.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("user")
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    @GetMapping("findByUser")
    public Result findByUser(String login_name, String ip) {
        Result result = new Result();
        User user = userDao.findByName(login_name);

        if (user == null){
            result.setCode("503");
        }else{
            result.setCode("200");
            result.setData(user);
            long time = new Date().getTime();
            //更新登录时间和IP地址
            userDao.updateUserLoginDateAndIp(time,ip,user.getLogin_name());
        }
        return result;
    }

    @Override
    @GetMapping("getByName")
    public User getByName(String username) {
        return userDao.findByName(username);
    }

    @Override
    @GetMapping("getUserAuthorityInfo")
    public String getUserAuthorityInfo(String username) throws Exception {
        Boolean flag = redisTemplate.hasKey("GrantedAuthority:" + username);

        if (flag) {
            if(username==null){
                throw new Exception();
            }
            // 优先从缓存获取
            String authority = (String) redisTemplate.opsForValue().get("GrantedAuthority:" + username);
            return authority;
        }else {

            String roles="";
            String perms="";
            //获取角色
            List<Map<String,String>> authorityList = userDao.getUserRoleByUname(username);

            for (Map<String, String> authority : authorityList) {

                int id=Integer.parseInt(String.valueOf(authority.get("id")) );
                String role = authority.get("roleName");

                roles = roles.concat("ROLE_"+role+",");
                //获取权限
                List<String> permList = userDao.getPermByRid(id);
                for (String perm : permList) {
                    perms = perms.concat(perm+",");
                }
            }
            roles = roles.concat(perms);

            redisTemplate.opsForValue().set("GrantedAuthority:" + username, roles, 60*60, TimeUnit.SECONDS);
            return roles;

        }

    }

    @Override
    @GetMapping("findByPerm")
    public List<String> findByPerm(String name) {

        List<String> perms = new ArrayList<>();

        List<Map<String, String>> userRole = userDao.getUserRoleByUname(name);

        for (Map<String, String> authority : userRole) {
            int id=Integer.parseInt(String.valueOf(authority.get("id")) );
            //获取权限
            List<String> permList = userDao.getPermByRid(id);
            for (String perm : permList) {
                perms.add(perm);
            }

        }

        return perms;
    }

    @Override
    @PostMapping("addNotice")
    public void addNotice(@RequestBody Map<String, String> maps) {
        userDao.addNotice(maps.get("head"),maps.get("content"),new Date());
    }

    @Override
    @GetMapping("selectNotice")
    public List<Map<String, String>> selectNotice(){
        List<Map<String, String>> notices = userDao.selectNotices();
        return notices;
    }

}
